package com.lin.activity;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import com.lin.utils.SharePreferenceUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.FeedBackActivity;
import com.yunfeng.supercall.activity.LoginActivity;
import com.yunfeng.supercall.activity.ShareActivity;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.UserBean;
import com.yunfeng.supercall.service.CallConnectionService;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SettingActivity extends BaseActivity implements OnClickListener {

	private LinearLayout mLinearLayoutChangePassWord;
	private LinearLayout mLinearLayoutPropse;
	private LinearLayout mLinearLayoutCheckUpdate;
	private LinearLayout mLinearLayoutClearCache;
	private LinearLayout mLinearLayoutAboutSuperCall;
	private Button mButtonQuit;
	private TextView mTextViewBindPhone;
	private FrameLayout mFrameLayoutMore;
	private FrameLayout mFrameLayoutBack;
	private LinearLayout mLinearLayoutShare;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting2);
		
		mLinearLayoutShare = (LinearLayout) findViewById(R.id.share);
		mLinearLayoutChangePassWord = (LinearLayout) findViewById(R.id.change_password);
		mLinearLayoutPropse = (LinearLayout) findViewById(R.id.propose);
		mLinearLayoutCheckUpdate = (LinearLayout) findViewById(R.id.check_update);
		mLinearLayoutClearCache = (LinearLayout) findViewById(R.id.clear_cache);
		mLinearLayoutAboutSuperCall = (LinearLayout) findViewById(R.id.about_supercall);
		mButtonQuit = (Button) findViewById(R.id.button_quit);
		mTextViewBindPhone = (TextView) findViewById(R.id.bind_phone);
		mFrameLayoutMore = (FrameLayout) findViewById(R.id.framelayout_more);
		mFrameLayoutBack = (FrameLayout) findViewById(R.id.framelayout_back);

		mFrameLayoutBack.setOnClickListener(this);
		mFrameLayoutMore.setOnClickListener(this);

		mLinearLayoutAboutSuperCall.setOnClickListener(this);
		mLinearLayoutClearCache.setOnClickListener(this);
		mLinearLayoutCheckUpdate.setOnClickListener(this);
		mLinearLayoutPropse.setOnClickListener(this);
		mLinearLayoutChangePassWord.setOnClickListener(this);
		mButtonQuit.setOnClickListener(this);
		mLinearLayoutShare.setOnClickListener(this);
		UserBean user = BaseActivity.getUser(getApplicationContext());
		mTextViewBindPhone.setText(user.getPhone());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == mLinearLayoutChangePassWord) {
			startActivity(new Intent(getActivity(),
					ResetPasswordActivity.class));
		} else if(v == mLinearLayoutCheckUpdate) {
			chechUpdate();
		} else if(v == mLinearLayoutClearCache) {
			ImageLoader.getInstance().clearDiskCache();
			ImageLoader.getInstance().clearMemoryCache();
			Toast.makeText(getApplicationContext(), "清理缓存成功", Toast.LENGTH_LONG).show();
		} else if(v == mLinearLayoutPropse) {
			startActivity(new Intent(getActivity(),
					FeedBackActivity.class));
		} else if(v == mLinearLayoutAboutSuperCall) {
			startActivity(new Intent(getActivity(),
					AboutActivity.class));
		} else if(v == mButtonQuit) {
			switchAccount();
		} else if(mFrameLayoutBack == v) {
			finish();
		} else if(mFrameLayoutMore == v) {
			Intent intent = new Intent(this, ShareActivity.class);
			startActivity(intent);
		} else if(mLinearLayoutShare == v) {
			share();
		}
	}

	private void chechUpdate() {
		UmengUpdateAgent
		.setUpdateListener(new UmengUpdateListener() {

			@Override
			public void onUpdateReturned(
					int updateStatus,
					UpdateResponse arg1) {
				// TODO Auto-generated method stub
				if (getActivity() == null) {
					return;
				}
				switch (updateStatus) {
				case UpdateStatus.Yes: // has update
					UmengUpdateAgent.showUpdateDialog(
							getActivity(), arg1);
					break;
				case UpdateStatus.No: // has no update
					Toast.makeText(getActivity(),
							R.string.no_update,
							Toast.LENGTH_SHORT).show();
					break;
				case UpdateStatus.NoneWifi: // none wifi
					Toast.makeText(getActivity(),
							R.string.update_only_wifi,
							Toast.LENGTH_SHORT).show();
					break;
				case UpdateStatus.Timeout: // time out
					Toast.makeText(getActivity(),
							R.string.time_out,
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		});
		UmengUpdateAgent.forceUpdate(getActivity());
	}

	public void switchAccount() {
		// UCSService.uninit(this);
		stopService(new Intent(this, CallConnectionService.class));
		SharePreferenceUtil.getInstance().deleteUserInfo(getApplicationContext());
		getLoginManager(SettingActivity.this).logout();
		// System.exit(0);
		Intent intent = new Intent(this, LoginActivity.class);
		intent.setAction(Constants.ACTION_FINSIH);
		startActivity(intent
		.addFlags(
				Intent.FLAG_ACTIVITY_CLEAR_TASK
				| Intent.FLAG_ACTIVITY_NEW_TASK).putExtra(
						"autologin", false));
		JPushInterface.setAlias(getApplicationContext(), "",
				new TagAliasCallback() {

			@Override
			public void gotResult(int arg0, String arg1,
					Set<String> arg2) {
			}
		});
		JPushInterface.stopPush(getApplicationContext());
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		UmengUpdateAgent.setUpdateListener(null);
	}

	
	public void share() {
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse("http://www.chaoruan.net/m/login.asp?suid="
				+ getUser(getActivity()).getSuid()));
		startActivity(i);
	}
}